package com.shengyin.ec.service.goods;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shengyin.ec.model.dao.goods.GoodsDao;
import com.shengyin.ec.model.entity.Goods;


@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
	
	@Autowired
	GoodsDao goodsDao;
	

	public void createGoods(Goods goods){
		goodsDao.save(goods);
	}
	
	public Goods getGoodsById(long id){
		return goodsDao.get(id);
	}

	@Override
	public List<Goods> getAllGoodsList() {
		@SuppressWarnings("unchecked")
		List<Goods> goodsList  =goodsDao.getAllGoodsList();
		return goodsList;
	}

	@Override
	public void removeGoodsByID(long id) {
		
		Goods goods = goodsDao.load(id);
		goodsDao.delete(goods);		
	}

	@Override
	public List<Goods> getGoodsList(int offset, int pagesize) {
		return goodsDao.getGoodsList(offset, pagesize);
	}

	@Override
	public List<Goods> getGoodsListByKeyword(String keyword, int start, int end) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Goods> getGoodsListByKeyword(String prop, String keyword,
			int start, int end) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void modifyGoods(Goods goods) {
		
		Goods goods1=goodsDao.get(goods.getId());
		try {
			BeanUtils.copyProperties(goods1, goods);
			goodsDao.update(goods1);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	@Override
	public List<Goods> getGoodsListByType(String typename, int start, int end) {
		return goodsDao.getGoodsListByType(typename, start, end);
	}

	@Override
	public void deleteSelectedGoods(String[] ids) {
		if(ids!=null){
			Long[] idsl=new Long[ids.length];
			for(int i=0;i<ids.length;i++){
				if(ids[i]!=null||!ids[i].isEmpty()){
					idsl[i]=Long.valueOf(ids[i].trim());
				}
				
			}
			goodsDao.deleteSelectedGoods(idsl);
			}
		
	}
	
	//add by lcl 2012-3-17
	@Override
	public Goods getGoodsByProductId(long productId){
		Goods goods = goodsDao.getGoodsByProductId(productId);
		return goods;
	}


	@Override
	public List<Goods> getAllGoodsList(int offset, int pagesize) {
		return null;
	}

	@Override
	public Long getTotal() {
		return goodsDao.getTotal();
	}
	
	//add wql 20100319
	public List<Goods> getGoodsListByType(String typeName){
		return goodsDao.getGoodsListByType(typeName);
	}
	
	//add wql 2012-3-25
	public List<Goods> checkGoodNameExist(String productName){
		return goodsDao.checkGoodNameExist(productName);
	}

	@Override
	public List<Goods> getGoodsList(long companyid, int offset, int pagesize) {
		return goodsDao.getGoodsList(companyid, offset, pagesize);
	}

	@Override
	public void createGoods(Goods goods, long companyid) {
		goodsDao.createGoods(goods,companyid);
		
	}

	@Override
	public List<Goods> getGoodsListBySelected(String[] ids) {
		if(ids!=null){
			Long[] idsl=new Long[ids.length];
			for(int i=0;i<ids.length;i++){
				if(ids[i]!=null){
					idsl[i]=Long.valueOf(ids[i].trim());
				}
				
			}
			return goodsDao.getGoodsListBySelected(idsl);
			}
		return null;
	}

    public List<Goods> getGoodsListByCondition(int type,String keyword,int offset,int pageSize){
    	return goodsDao.getGoodsListByCondition(type,keyword,offset,pageSize);
    }
	
	public List<Goods> getGoodsListByCondition(int type,String keyword){
		return goodsDao.getGoodsListByCondition(type,keyword);
	}
	
	@Override
	public void changeImage(Long id, String image) {
		goodsDao.changeImage(id, image);
	}

}
